package com.company;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Task5 {
    public static void main(String[] args) {
        //todo 请定义一个登录接口的业务处理类，并编写一个login方法，来模拟处理如下登录异常情况：
        // 1001-用户名或密码为空
        // 1002-用户名不存在
        // 1003-密码错误
        // 1004-此帐号已锁定
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入账号：");
        String userName = scanner.next();
        System.out.println("请输入密码：");
        String pwd = scanner.next();
        try {
            login(userName, pwd);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void login(String userName, String pwd) throws Exception {
        if (userName.isEmpty() || pwd.isEmpty()) {
            throw new NullPointerException("用户名或密码为空");
        }

        //模拟数据库中的user表数据
        ArrayList al = new ArrayList();

        String[] user1 = new String[]{"qmai", "qmai", "1"}; //{用户名，密码，状态：1可用、0锁定}
        String[] user2 = new String[]{"qimai", "qimai", "1"};
        String[] user3 = new String[]{"admin", "123456", "1"};
        String[] user4 = new String[]{"root", "root", "0"};
        al.add(user1);
        al.add(user2);
        al.add(user3);
        al.add(user4);

        boolean b = false; //判断登录状态
        for (int i = 0; i < al.size(); i++) {
            String[] user = (String[]) al.get(i);
            if (userName.equals(user[0])) { //用户名匹配
                if (!pwd.equals(user[1])) { //密码不匹配
                    throw new Exception("用户" + userName + ",密码错误");
                } else if (user[2].equals("0")) {
                    throw new Exception("此帐号已锁定");
                } else {
                    b = true; //登录成功
                }

            }
        }
        if (!b) {
            throw new Exception("用户名不存在");
        }else{
            System.out.println("登录成功");
        }

    }
}
